﻿using Core;
using Core.Entities;
using Core.Tools;
using Core.Writer;
using System;

namespace Api.Commands.Commons
{
    public sealed class ReadPagingData : ISystemCommand
    {
        public string Name
        {
            get
            {
                return "ReadPagingData";
            }
        }

        public void Process(BaseProcess Process, CommandInfo CommandInfo, IDataWriter Writer)
        {
            string TableName = Process.GetItem<string>("TableName");

            CommandInfo ReadPageCommand = Process.GetCommand("ReadPage" + TableName);

            Process.CheckCommand(ReadPageCommand);

            Writer["Data"] = Process.ProcessCommand<Table>(ReadPageCommand);

            bool WithTotal = Convert.ToBoolean(Process["WithTotal"]);

            if (WithTotal)
            {
                CommandInfo GetTotalCommand = Process.GetCommand("Get" + TableName + "Total");

                Process.CheckCommand(GetTotalCommand);

                Writer["Total"] = Process.ProcessCommand<TotalBox>(GetTotalCommand).Total;
            }
        }

        public sealed class TotalBox
        {
            public int Total { get; set; }
        }
    }
}